package mathive.server;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.jdo.PersistenceManager;

import mathive.client.ClientSudokuData;
import mathive.client.SudokuService;
import mathive.server.persistant.SudokuData;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SudokuServiceImpl extends RemoteServiceServlet implements SudokuService {

	public ClientSudokuData getSudoku(int id, int difficulty) {

		PersistenceManager pm = PMF.getPersistenceManager();

		String query;
		List<SudokuData> sudokus;
		SudokuData oldSudoku;
		
		if(id != -1){
			query = "select from " + SudokuData.class.getName() + " where id == " + id;		
			sudokus = (List<SudokuData>) pm.newQuery(query).execute();
			if(sudokus.isEmpty()){		// If there is no sudoku with that id
				return null;
			}
			oldSudoku = sudokus.get(0);
		} else {
			query = "select from " + SudokuData.class.getName() + " where difficulty == " + difficulty;

			sudokus = (List<SudokuData>) pm.newQuery(query).execute();
			if(sudokus.isEmpty()){		// If there is no sudoku with that difficulty
				return null;
			}
			Random random = new Random();
			oldSudoku = sudokus.get(random.nextInt(sudokus.size()));
		}
		int newId = oldSudoku.getId();
		String newSudoku = oldSudoku.getSudoku();
		int newDifficulty = oldSudoku.getDifficulty();

		ClientSudokuData newSudokuData = new ClientSudokuData(newId, newSudoku, newDifficulty);

		pm.close();

		return newSudokuData;
	}

	public String saveSudoku(int difficulty, String sudoku) {

		PersistenceManager pm = PMF.getPersistenceManager();


		String query = "select from " + SudokuData.class.getName() + " order by id";
		List<SudokuData> sudokus = (List<SudokuData>) pm.newQuery(query).execute();
		int size = sudokus.size();
		int id = size;
		for (int i = 0; i < size; i++) {
			if(sudokus.get(i).getId() != i){
				id = i;
				break;
			}
		}

		SudokuData sudokuData = new SudokuData(id, sudoku, difficulty);

		try {
			pm.makePersistent(sudokuData);
		} finally {
			pm.close();
		}
		return Integer.toString(id);
	}
	
	public boolean deleteSudoku(int id){
		
		PersistenceManager pm = PMF.getPersistenceManager();

		String query = "select from " + SudokuData.class.getName()+ " where id == " + id;
		List<SudokuData> sudokus = (List<SudokuData>) pm.newQuery(query).execute();
		SudokuData sudokuData = sudokus.get(0);

		try {
			pm.deletePersistent(sudokuData);
		} finally {
			pm.close();
		}
		return true;
	}
	
	public String[] listSudokus(){
		String[] sudokus;
		
		PersistenceManager pm = PMF.getPersistenceManager();

		String query = "select from " + SudokuData.class.getName() + " order by id";
		List<SudokuData> sudokuList = (List<SudokuData>) pm.newQuery(query).execute();
		sudokus = new String[sudokuList.size()];
		Iterator<SudokuData> listIterator = sudokuList.iterator();
		int i = 0;
		while(listIterator.hasNext()){
			sudokus[i] = listIterator.next().getString();
			i++;
		}
		
		pm.close();
		
		return sudokus;
	}

}
